/**
 * Created by Administrator on 2016/1/5.
 */
angular.module('bluetoothoptions.controller',[])
  .controller('blueToothOptionsCtrl',['$scope','$q', '$ionicPopup', '$ionicLoading','$localstorage','$state','blueToothOptionsService','$cordovaBluetoothSerial','$timeout',function($scope,$q,$ionicPopup,$ionicLoading,$localstorage,$state,blueToothOptionsService,$cordovaBluetoothSerial,$timeout){

    $scope.params=blueToothOptionsService.getParams();

    $scope.$on('$ionicView.enter', function(event, data) {
      //$scope.initDevices();
      $scope.params.bluetoothDevices=[];

    });

    //判断蓝牙设备是否打开
    $scope.isEnabled = function () {
      console.log("正在检测蓝牙是否打开");

      $cordovaBluetoothSerial.isEnabled().then(function(data){
        console.log("返回正确");
        console.log(angular.fromJson(data));
        $scope.params.bluetoothStatus="已开";
      },function(error){
        console.log("返回失败");
        console.log(angular.fromJson(error));
        $scope.params.bluetoothStatus="未开";
      });
      console.log("蓝牙结束");
    };
    //设备初始化
    $scope.initDevices=function()
    {
      console.log('设备已经就绪');
      var options = {};
      options.deviceType = ['2', '4', '8'];
      options.pluginID = '201605201032HuBei';
      BLEIDCardPlugin.ai_blPluginRegister(function (result) {

      }, function (error) {

      }, JSON.stringify(options));
    }

    //搜索蓝牙设备
    $scope.searchBtDevices=function()
    {
      /*设备列表刷新*/
      var options = {};
      options.deviceType = ['2', '4', '8'];
      console.log('设备列表扫描:' + JSON.stringify(options));

      $ionicLoading.show({
        template: '<ion-spinner icon="ios"></ion-spinner>',
        content: '拼命加载中，请耐心等候...',
        animation: 'fade-in',
        showBackdrop: false,
        maxWidth: 200,
        showDelay: 0
      });

      BLEIDCardPlugin.ai_refreshDeviceList(function (result) {
        console.log('扫描到的设备列表如下:' + JSON.stringify(result) + '设备个数:' + result.devices.length);
        $timeout(function(){
          $scope.params.bluetoothDevices=result.devices;
          $ionicLoading.hide();
        },1000);
      }, function (error) {
        console.error('设备扫描失败' + JSON.stringify(error));
        $ionicLoading.hide();
      }, JSON.stringify(options));

    }

    //匹配蓝牙设备
    //点击蓝牙的每一个item 弹出一个item ,然后点击保存,这时将当前的item保存在$localstorage,并且调用插件连接蓝牙设备
    $scope.doPaireDevices=function(item)
    {
      $scope.params.defaultBluetoothDevice=item;
      var options = {};
      options.name = $scope.params.defaultBluetoothDevice.name;

      var confirmPopup = $ionicPopup.confirm({
        title: '设置确认',
        template: '确认将'+ options.name + '设为默认连接蓝牙读卡器吗？',
        buttons: [
          { text: '取消' },
          {text: '保存',
            type: 'button-positive',
            onTap: function(e){return true;}
          }
        ]
      });
      confirmPopup.then(function(res) {
        if(res) {
          BLEIDCardPlugin.ai_connectDevice(function (result) {
            $localstorage.set('btdevicename', $scope.params.defaultBluetoothDevice.name);
            console.log('读卡器连接成功');
          }, function (error) {
            console.log('读卡器连接失败');
          }, JSON.stringify(options));

        } else {

        }
      });
    }
  }]);
